/**
 *  Copyright (C) 2006 zhangbo (freeeob@gmail.com)
 *
 *  This product is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 * 
 *  This product is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 *
 *  author:zhangbo
 *  Email:jsjava@gmail.com
 */

/**
 * constructor
 */
function HtmlPrint(){
	  this.jsjava_class="jsorg.eob.component.print.HtmlPrint";
}

/**
 * Set the print area
 * @param area the print area
 */
HtmlPrint.prototype.setArea=function(area){
	this.area=area;
};

/**
 * preview the print result
 * @param title the title of the preview window
 * @param preWinFeatures the preview window features such as toolsbar=no,menubar=no and so on
 */
HtmlPrint.prototype.preview=function(title,preWinFeatures){
	var isNoneIE=navigator.userAgent.indexOf("MSIE")==-1;
	var areaId=this.area.getAreaId();
	var headObj=document.getElementsByTagName("HEAD");
	if(headObj){
		headObj=headObj[0];
	}
	var areaObj=document.getElementById(areaId);
	var preWin=window.open("about:blank","_blank",preWinFeatures);
	var preWinConent="<html><head>";
	preWinConent+=headObj.innerHTML;
	preWinConent+="</head><body>";
	preWinConent+=areaObj.outerHTML;
	preWinConent+="</body></html>";
	if(isNoneIE){
		preWin.document.open();
		preWin.document.write(preWinConent);
		reWin.document.close();
	}else{
		preWin.document.body.innerHTML=preWinConent;
	}
	if(title!=undefined){
		preWin.document.title=title;
	}
};

/**
 * print the result
 */
HtmlPrint.prototype.print=function(){  
	var isNoneIE=navigator.userAgent.indexOf("MSIE")==-1;
    var iframeElem=null;
    if(isNoneIE){
	    iframeElem=document.createElement("iframe");
	    iframeElem.setAttribute("name","jsjava_print_iframe");
	    iframeElem.setAttribute("src","about:blank");
	    iframeElem.setAttribute("style","position:absolute;top:-5;left:-5;display:inline;width:0;height:0");
    }else{
    	iframeElem=document.createElement("<iframe name='jsjava_print_iframe' src='about:blank' style='position:absolute;top:-5;left:-5;display:inline;width:0;height:0'></iframe>"); 
    }
    document.body.appendChild(iframeElem);
    var areaId=this.area.getAreaId();
	var headObj=document.getElementsByTagName("HEAD");
	if(headObj){
		headObj=headObj[0];
	}
	var areaObj=document.getElementById(areaId);	    
	var preWinConent="<html><head>";
	preWinConent+=headObj.innerHTML;
	preWinConent+="</head><body>";
	preWinConent+=areaObj.outerHTML;
	preWinConent+="</body></html>";
	var preWin=window.jsjava_print_iframe;
	if(!preWin.document||!preWin.document.body){
		setTimeout("jsjava_htmlprint_print('"+areaId+"')",200);
	}else{
		preWin.document.body.innerHTML=preWinConent;
		preWin.focus();
	    preWin.print();
	}	
};

function jsjava_htmlprint_print(areaId){
	var headObj=document.getElementsByTagName("HEAD");
	if(headObj){
		headObj=headObj[0];
	}
	var areaObj=document.getElementById(areaId);	    
	var preWinConent="<html><head>";
	preWinConent+=headObj.innerHTML;
	preWinConent+="</head><body>";
	preWinConent+=areaObj.outerHTML;
	preWinConent+="</body></html>";
	var preWin=window.jsjava_print_iframe;
	if(!preWin.document||!preWin.document.body){
		setTimeout("jsjava_htmlprint_print('"+areaId+"')",200);
	}else{
		preWin.document.body.innerHTML=preWinConent;
		preWin.focus();
	    preWin.print();
	}
}